<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Company"%>
<%@page import="db.CompanyDAO"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>

<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	CompanyDAO companyDAO = (CompanyDAO)ctx.getBean("CompanyDAO");
	
	
	int tid = (Integer)session.getAttribute("tid");
	
	String name = request.getParameter("cname");
	String receiver1 = request.getParameter("receiver1");
	String receiver2 = request.getParameter("receiver2");
	String receiver3 = request.getParameter("receiver3");
	String receiver4 = request.getParameter("receiver4");
	String receiver5 = request.getParameter("receiver5");
	String phone = request.getParameter("phone");
	String identity = request.getParameter("identity");
	String remark = request.getParameter("remark");
	
	String type = request.getParameter("type");
	request.setAttribute("type",type);
	String message = "";
	
	
	if(type=="2" || "2".equals(type)){
		if(name==null || "".equals(name)){
				message = "添加施工单位失败，单位名称不能为空。";
				request.setAttribute("message",message);
				request.setAttribute("suess",false);
				pageContext.forward("/vm/coninfo/add_coninfo.vm");
				return;
		}
		
		List md = companyDAO.findNameAndTeamId(name,tid);
		if(md != null && md.size() > 0){
			message = "添加施工单位失败，单位名称已经存在。";
			request.setAttribute("suess",false);
			request.setAttribute("message",message);
			pageContext.forward("/vm/coninfo/add_coninfo.vm");
			return;
		}
		
		if(phone==null){
			phone="";
		}
		if(identity==null){
			identity="";
		}
		if(remark==null){
			remark="";
		}
		
		
		
		Company co = new Company();
		co.setDate(new Date());
		co.setIdentity(identity);
		co.setIsDelete(0);
		co.setName(name);
		co.setPhone(phone);
		co.setReceiver1(receiver1);
		co.setReceiver2(receiver2);
		co.setReceiver3(receiver3);
		co.setReceiver4(receiver4);
		co.setReceiver5(receiver5);
		co.setRemark(remark);
		co.setTeamId(tid);
		companyDAO.save(co);
		message = "恭喜您！添加施工单位成功!";
		request.setAttribute("message",message);
		request.setAttribute("suess",true);
		pageContext.forward("/vm/coninfo/add_coninfo.vm");
	}else{
		pageContext.forward("/vm/coninfo/add_coninfo.vm");
	}
	
%>
